package com.th.supcom.pds.deptdispensary.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.framework.dict.web.gwt.client.model.DictModel;
import com.th.supcom.framework.organization.web.gwt.client.model.OrganizationModel;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.deptdispensary.client.model.DeptVsDispensaryModel;
import com.th.supcom.pds.deptdispensary.client.service.DeptVsDispensaryRpc;
import com.th.supcom.pds.deptdispensary.client.service.DeptVsDispensaryRpcAsync;

/**
 * 药品材质药房Action
 * 
 * @author 杨冲
 */
public class PhamMeterialTypeAction
{
    private DeptVsDispensaryFields m_fields;
    private PhamMaterialTypeGrid m_grid;
    private PhamMeterialTypeEditWindow m_window;
    private final DeptVsDispensaryRpcAsync s_deptVsDispensaryRpcAsync = GWT.create (DeptVsDispensaryRpc.class);

    public PhamMeterialTypeAction ()
    {

    }

    /**
     * 设置护理单元药房文本组件
     * 
     * @param fields
     */
    public void setFields (DeptVsDispensaryFields fields)
    {

        this.m_fields = fields;
    }

    /**
     * 设置Grid
     * 
     * @param grid
     */
    public void setGrid (PhamMaterialTypeGrid grid)
    {

        m_grid = grid;
    }

    public void setWindow (PhamMeterialTypeEditWindow window)
    {

        m_window = window;
    }

    /**
     * 编辑护理单元药房配置关系的时候初始化数据
     */
    public void initDataWhenEdit ()
    {
        DeptVsDispensaryModel model = m_grid.getGrid ().getSelectionModel ().getSelectedItem ();
        if (null == model)
        {
            return;
        }

        Long id = model.getDeptVsDispensaryId ();

        s_deptVsDispensaryRpcAsync.getDeptVsDispensaryById (id, new AsyncCallback <DeptVsDispensaryModel> ()
        {
            @Override
            public void onSuccess (DeptVsDispensaryModel result)
            {
                initDataWhenEdit (result);
            }

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }
        });
    }

    /**
     * 编辑的时候初始化数据
     */
    private void initDataWhenEdit (DeptVsDispensaryModel model)
    {

        OrganizationModel dispensaryModel = new OrganizationModel ();
        dispensaryModel.setCode (model.getDispensary ());
        dispensaryModel.setName (model.getDispensaryName ());
        m_fields.getDispensaryBox ().setValue (dispensaryModel);

        DictModel phamMaterialTypeModel = new DictModel ();
        phamMaterialTypeModel.setDictItemCode (model.getPhamMaterialType ());
        phamMaterialTypeModel.setDictItemName (model.getPhamMaterialTypeName ());
        m_fields.getPhamMaterialBox ().setValue (phamMaterialTypeModel);
    }

    /**
     * 获取护理单元药房配置关系
     * 
     * @return
     */
    private DeptVsDispensaryModel getDeptVsDispensaryModel (Boolean isEdit)
    {
        DeptVsDispensaryModel model = new DeptVsDispensaryModel ();
        if (isEdit)
        {
            model.setDeptVsDispensaryId (m_grid.getGrid ().getSelectionModel ().getSelectedItem ().getDeptVsDispensaryId ());
        }
        model.setDispensary (m_fields.getDispensaryBox ().getValue ().getCode ());
        model.setPhamMaterialType (m_fields.getPhamMaterialBox ().getValue ().getDictItemCode ());
        return model;
    }

    /**
     * 新增护理单元药房配置关系
     * 
     * @param model
     */
    private void addPhamMeterialType (final boolean isAddContinue)
    {
        DeptVsDispensaryModel model = getDeptVsDispensaryModel (false);
        s_deptVsDispensaryRpcAsync.addPhamMeterialType (model, new AsyncCallback <Void> ()
        {

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }

            @Override
            public void onSuccess (Void result)
            {
                Info.display (CodeConstants.PROMPT, "添加成功");
                refresh ();
                if (isAddContinue)
                {
                    clearEditWindow ();
                }
                else
                {
                    m_window.hide ();
                }
            }
        });
    }

    /**
     * 编辑护理单元药房关系
     */
    private void editPhamMeterialType ()
    {
        DeptVsDispensaryModel model = getDeptVsDispensaryModel (true);
        s_deptVsDispensaryRpcAsync.modifyPhamMeterialTypeByExample (model, new AsyncCallback <Void> ()
        {

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }

            @Override
            public void onSuccess (Void result)
            {
                Info.display (CodeConstants.PROMPT, "更新成功");
                refresh ();
                m_window.hide ();
            }
        });
    }

    /**
     * 编辑护理单元药房关系
     * 
     * @param isEdit
     */
    public boolean editPhamMeterialType (boolean isEdit, boolean isAddContinue)
    {
        if (!validate ())
        {
            return false;
        }
        if (isEdit)
        {
            editPhamMeterialType ();
        }
        else
        {
            addPhamMeterialType (isAddContinue);
        }
        return true;
    }

    /**
     * 清除编辑窗口
     */
    public void clearEditWindow ()
    {
        m_fields.getDispensaryBox ().clear ();
        m_fields.getPhamMaterialBox ().clear ();
    }

    /**
     * 刷新数据
     */
    public void refresh ()
    {
        DeptVsDispensaryModel model = new DeptVsDispensaryModel ();
        s_deptVsDispensaryRpcAsync.findPhamMaterialTypeListByExample (model, new AsyncCallback <List <DeptVsDispensaryModel>> ()
        {

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }

            @Override
            public void onSuccess (List <DeptVsDispensaryModel> result)
            {
                m_grid.getGrid ().getStore ().removeAll ();
                m_grid.getGrid ().getStore ().add (result);
            }
        });
    }

    /**
     * 验证组件是否为空
     * 
     * @return
     */
    private boolean validate ()
    {
        List <TextField < ? >> fields = new ArrayList <TextField < ? >> ();
        fields.add (m_fields.getDispensaryBox ());
        fields.add (m_fields.getPhamMaterialBox ());

        List <String> messages = new ArrayList <String> ();
        messages.add ("药房不能为空");
        messages.add ("药品材质");

        return ClientUtils.validateFields (fields, messages);
    }

    /**
     * 判断Grid是否选中
     * 
     * @return
     */
    public boolean getGridSelectedStatus ()
    {
        DeptVsDispensaryModel model = m_grid.getGrid ().getSelectionModel ().getSelectedItem ();
        if (null == model)
        {
            return false;
        }
        return true;

    }

    /**
     * 删除选中的内容
     */
    public void delete ()
    {
        DeptVsDispensaryModel model = m_grid.getGrid ().getSelectionModel ().getSelectedItem ();
        Long id = model.getDeptVsDispensaryId ();
        s_deptVsDispensaryRpcAsync.removePhamMeterialTypeByID (id, new AsyncCallback <Void> ()
        {

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }

            @Override
            public void onSuccess (Void result)
            {
                Info.display (CodeConstants.PROMPT, "删除成功");
                refresh ();
            }
        });
    }

}
